
package controllers;

import java.util.ArrayList;
import java.util.List;

import models.spider.actor.CrawlerService;
import models.spider.config.AAA;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.search.jpa.FullTextQuery;

import play.data.Form;
import play.db.jpa.Transactional;
import play.mvc.Result;

import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import play.libs.Json;
import play.libs.Json.*;
import static play.libs.Json.toJson;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;
import play.mvc.BodyParser;
import play.Logger;

import models.aaa.Asset;
import models.re.AAAContext;
import models.re.Re;

/**
 * 
 * @author laurent@opprecht.info
 */
public class Widget extends Application {

    public static class TextEntry {

        public String text;
    }

    public static CrawlerService crawler() {
        CrawlerService result = new CrawlerService(new AAA());
        return result;
    }

//    @SuppressWarnings("unchecked")
//    @Transactional(readOnly = true)
//    public static Result index(final String user, final String search) {
//        models.aaa.Asset actor = new models.aaa.Asset();
//        if (user != null) {
//            actor = models.aaa.Asset.find.byIdentifier("pid", user);
//        } else {
//            actor = getUser().getActor();
//        }
//        
//        Form<TextEntry> form = play.data.Form.form(TextEntry.class).bindFromRequest();

//        List<models.aaa.Asset> items = new ArrayList<>();
//        List<models.aaa.Asset> lo = new ArrayList<>();
//        List<models.aaa.Asset> users = new ArrayList<>();

//        //String text = form.get().text;
//        String text = search;
//        if (StringUtils.isEmpty(text)) {
//            return ok(views.html.widget.index.render(form, items, lo, users, actor, user));
//        }

//        FullTextQuery query_activities = models.aaa.Asset.find.withType(text, actor, "SPACE");
//        FullTextQuery query_lo = models.aaa.Asset.find.withType(text, actor, "ASSET");
//        FullTextQuery query_users = models.aaa.Asset.find.withType(text, actor, "ACTOR");
//        
//        if (query_activities == null && query_lo == null && query_users == null) {
//            flash("warning", "no result found");
//            return ok(views.html.widget.index.render(form, items, lo, users, actor, user));
//        }

//        if (query_activities != null) {
//                items = query_activities.setMaxResults(3).getResultList();
//        }
//        if (query_lo != null) {
//                lo = query_lo.setMaxResults(3).getResultList();
//        }
//        if (query_users != null) {
//                users = query_users.setMaxResults(3).getResultList();
//        }
//        
//        if (items.size() == 0 && lo.size() == 0 && users.size() == 0) {
//            flash("warning", "no result found");
//        }

//        return ok(views.html.widget.index.render(form, items, lo, users, actor, user));

//    }

    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    public static Result index(final String user, final String context) {
        models.aaa.Asset actor = new models.aaa.Asset();
        if (user != null) {
            //actor = models.aaa.Asset.find.byIdentifier("pid", user);
            actor = models.aaa.Asset.find.byLid(user);
        } else {
            actor = getUser().getActor();
        }
        
        Form<TextEntry> form = play.data.Form.form(TextEntry.class).bindFromRequest();

        List<models.aaa.Asset> items = Re.getRecommendation2(new AAAContext(context, "SPACE"), user, 3);
        List<models.aaa.Asset> lo    = Re.getRecommendation2(new AAAContext(context, "ASSET"), user, 3);
        List<models.aaa.Asset> users = Re.getRecommendation2(new AAAContext(context, "ACTOR"), user, 3);

        return ok(views.html.widget.index.render(form, items, lo, users, actor, user));

    }







    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    public static Result big(final String user, final String search) {
        models.aaa.Asset actor = new models.aaa.Asset();
        if (user != null) {
            actor = models.aaa.Asset.find.byIdentifier("pid", user);
        } else {
            actor = getUser().getActor();
        }
        
        String text;
        if (search == null) {
            text = "que faire"; // TODO: que faire quand la recherche est vide...
        } else {
            text = search;
        }
        
        Form<TextEntry> form = play.data.Form.form(TextEntry.class).bindFromRequest();

        List<models.aaa.Asset> spaces = new ArrayList<>();
        List<models.aaa.Asset> users = new ArrayList<>();
        List<models.aaa.Asset> assets = new ArrayList<>();

        FullTextQuery query_spaces = models.aaa.Asset.find.withType(text, actor, "SPACE");
        FullTextQuery query_users = models.aaa.Asset.find.withType(text, actor, "ACTOR");
        FullTextQuery query_assets = models.aaa.Asset.find.withType (text, actor, "ASSET");

        spaces = query_spaces.setMaxResults(6).getResultList();
        users = query_users.setMaxResults(6).getResultList();
        assets = query_assets.setMaxResults(6).getResultList();

        return ok(views.html.widget.big.render(form, spaces, users, assets, actor, user));

    }

    @play.db.jpa.Transactional
    public static Result bigSearch() {
        Form<TextEntry> form = play.data.Form.form(TextEntry.class).bindFromRequest();
//        String user = form.data().get("user");
        String search = form.get().text;
        if (search == "") {
//            return redirect(controllers.routes.Widget.big(user, null));
            return redirect("/bigwidget");
        }
        return big("user", search);//TODO changer "user"...
    }

    @play.db.jpa.Transactional
    @BodyParser.Of(BodyParser.Json.class)
    public static Result ajax(final String search, final String type)
    {
        List<models.aaa.Asset> assets = new ArrayList<>();
        models.aaa.Asset actor = getUser().getActor();

        FullTextQuery query = models.aaa.Asset.find.withType(search, actor, type);

        assets = query.setMaxResults(6).getResultList();
        
        Gson gson = new Gson();
            
        List<Map> list = new ArrayList<>();
        for (models.aaa.Asset asset : assets) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("name", asset.getName());
            map.put("url", "/asset/"+asset.getId());
            list.add(map);
        }
        String result = gson.toJson(list);
        return ok(result);
    }
    
    @SuppressWarnings("unchecked")
	@play.db.jpa.Transactional
    public static Result elgg(final String user, final String item, final String search) {
    	System.out.println(user);
    	models.aaa.Asset userAsset = null;
    	if(!user.isEmpty()) userAsset = models.aaa.Asset.find.byLid(user);
    	Form<TextEntry> form = play.data.Form.form(TextEntry.class).bindFromRequest();
    	List<models.aaa.Asset> searches = new ArrayList<models.aaa.Asset>();
    	List<models.aaa.Asset> recs = new ArrayList<models.aaa.Asset>();
    	if(!item.isEmpty())    		
    		recs =  Re.getRecommendation(new AAAContext(item,userAsset), 10);
    	if(!search.isEmpty()) {
    		FullTextQuery query = models.aaa.Asset.find.with(search,userAsset);
    		if(query!=null) searches = query.setMaxResults(10).getResultList();
    		for(models.aaa.Asset s: searches) System.out.println(s.getLid());
    	}
    	return ok(views.html.widget.elgg.render(form, searches, recs, user));
    }

}
